<?
if (!defined('SMF'))
	die('Hacking attempt...');

function traderoute_die($error_msg)
{
	echo '<p>', $error_msg, '</p>';
	TextMainMenu();
	die();
}

function traderoute_check_compatible($type1, $type2, $move, $circuit, $src, $dest)
{
	global $smcFunc, $txt, $scripturl, $user_info;
	global $dbtables, $color_line1, $color_line2, $color_header;

	// Verify user login, if CheckLogin() returns false return to the calling script/function
	if (CheckLogin() == false)
		die();
	
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE user_id = {int:user_id}
		LIMIT 1',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$playerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	//check warp links compatibility
	if ($move == 'warp')
	{
		$result = $smcFunc['db_query']('', '
			SELECT link_id
			FROM {raw:db_links}
			WHERE link_start = {int:src}
			AND link_dest = {int:dest}',
			array(
				'db_links' => $dbtables['links'],
				'src' => $src['sector_id'],
				'dest' => $dest['sector_id'],
			)
		);
		
		if ($smcFunc['db_num_rows']($result) == 0)
		{
			$txt['bnt_tdr_nowlink1'] = str_replace("[tdr_src_sector_id]", $src['sector_id'], $txt['bnt_tdr_nowlink1']);
			$txt['bnt_tdr_nowlink1'] = str_replace("[tdr_dest_sector_id]", $dest['sector_id'], $txt['bnt_tdr_nowlink1']);
			echo '<input type="hidden" id="status" value="', $txt['bnt_tdr_nowlink1'], '" />';
			die();
		}
		
		$smcFunc['db_free_result']($result);
		
		if ($circuit == '2')
		{
			$result = $smcFunc['db_query']('', '
				SELECT link_id
				FROM {raw:db_links}
				WHERE link_start = {int:dest}
				AND link_dest = {int:src}',
				array(
					'db_links' => $dbtables['links'],
					'src' => $src['sector_id'],
					'dest' => $dest['sector_id'],
				)
			);
			if ($smcFunc['db_num_rows']($result) == 0)
			{
				$txt['bnt_tdr_nowlink2'] = str_replace("[tdr_src_sector_id]", $src['sector_id'], $txt['bnt_tdr_nowlink2']);
				$txt['bnt_tdr_nowlink2'] = str_replace("[tdr_dest_sector_id]", $dest['sector_id'], $txt['bnt_tdr_nowlink2']);
				echo '<input type="hidden" id="status" value="', $txt['bnt_tdr_nowlink2'], '" />';
				die();
			}
		}
	}

	// Check ports compatibility
	if ($type1 == 'port')
	{
		if ($src['port_type'] == 'special')
		{
			if (($type2 != 'planet') && ($type2 != 'corp_planet'))
			{
				echo '<input type="hidden" id="status" value="', $txt['bnt_tdr_sportissrc'], '" />';
				die();
			}
			if ($dest['owner'] != $playerinfo['ship_id'] && ($dest['corp'] == 0 || ($dest['corp'] != $playerinfo['team'])))
			{
				echo '<input type="hidden" id="status" value="', $txt['bnt_tdr_notownplanet'], '" />';
				die();
			}
		}
		else
		{
			if ($type2 == 'planet')
			{
				echo '<input type="hidden" id="status" value="', $txt['bnt_tdr_planetisdest'], '" />';
				die();
			}
			if ($src['port_type'] == $dest['port_type'])
			{
				echo '<input type="hidden" id="status" value="', $txt['bnt_tdr_samecom'], '" />';
				die();
			}
		}
	}
	elseif ($type2 == 'port')
	{
		if ($dest['port_type'] == 'special')
		{
			if (($type1 != 'planet') && ($type1 != 'corp_planet'))
			{
				echo '<input type="hidden" id="status" value="', $txt['bnt_tdr_sportissrc'], '" />';
				die();
			}
			if ($src['owner'] != $playerinfo['ship_id'] && ($src['corp'] == 0 || ($src['corp'] != $playerinfo['team'])))
			{
				echo '<input type="hidden" id="status" value="', $txt['bnt_tdr_notownplanet'], '" />';
				die();
			}
		}
		else
		{
			if ($type1 == 'planet')
			{
				echo '<input type="hidden" id="status" value="', $txt['bnt_tdr_planetisdest'], '" />';
				die();
			}
			if ($dest['port_type'] == $src['port_type'])
			{
				echo '<input type="hidden" id="status" value="', $txt['bnt_tdr_samecom'], '" />';
				die();
			}
		}
	}
	else
	{
		if ($dest['port_type'] == 'special')
		{
			echo '<input type="hidden" id="status" value="', $txt['bnt_tdr_sportcom'], '" />';
			die();
		}
	}
}


function traderoute_distance($type1, $type2, $start, $dest, $circuit, $sells = 'N')
{
	global $smcFunc, $txt, $scripturl, $user_info;
	global $dbtables, $color_line1, $color_line2, $color_header;
	global $level_factor;
	global $debug;
	
	// Verify user login, if CheckLogin() returns false return to the calling script/function
	if (CheckLogin() == false)
		die();
	
	// Pull player info.
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE user_id = {int:user_id}
		LIMIT 1',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$playerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	
	$retvalue['triptime'] = 0;
	$retvalue['scooped'] = 0;

	if ($type1 == 'L')
	{
		$result = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_universe}
			WHERE sector_id = {int:start}',
			array(
				'db_universe' => $dbtables['universe'],
				'start' => $start
			)
		);
		$start = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);
	}

	if ($type2 == 'L')
	{
		$result = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_universe}
			WHERE sector_id = {int:dest}',
			array(
				'db_universe' => $dbtables['universe'],
				'dest' => $dest
			)
		);
		$dest = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);
	}
	
	// Check if player is not changing sectors
	if ($start['sector_id'] == $dest['sector_id'])
	{
		if ($circuit == '1')
			$retvalue['triptime'] = '1';
		else
			$retvalue['triptime'] = '2';
		return $retvalue;
	}
	
	// If in a traderoute loop, RS movement calculation will be the same going there and coming back
	$deg = pi() / 180;
	$sa1 = $start['angle1'] * $deg;
	$sa2 = $start['angle2'] * $deg;
	$fa1 = $dest['angle1'] * $deg;
	$fa2 = $dest['angle2'] * $deg;
	$x = $start['distance'] * sin($sa1) * cos($sa2) - $dest['distance'] * sin($fa1) * cos($fa2);
	$y = $start['distance'] * sin($sa1) * sin($sa2) - $dest['distance'] * sin($fa1) * sin($fa2);
	$z = $start['distance'] * cos($sa1) - $dest['distance'] * cos($fa1);
	$distance = round(sqrt(mypw($x, 2) + mypw($y, 2) + mypw($z, 2)));
	$shipspeed = mypw($level_factor, $playerinfo['engines']);
	$triptime = round($distance / $shipspeed);
	
	if (!$triptime && $dest['sector_id'] != $playerinfo['sector'])
		$triptime = 1;
	
	if ($playerinfo['dev_fuelscoop'] == 'Y')
	{
		$free_power = NUM_ENERGY($playerinfo['power']) - $playerinfo['ship_energy'];
		$retvalue['scooped'] = energy_scooped($playerinfo, $distance, $triptime, $free_power);
	}

	if ($circuit == '2')
	{
		$triptime *= 2;
		$triptime += 2;
	}
	else
		$triptime += 1;

	$retvalue['triptime'] = $triptime;

	return $retvalue;
}

function traderoute_results_show_repeat($engage)
{
	global $scripturl;
	
    echo '
					<p>
						<form action="', $scripturl, '?action=blacknova;sa=traderoute;engage=', $engage, '" method="post">
						<label for="tr_repeat">Enter times to repeat</label><input type="text" name="tr_repeat" value="1" size="5">
						<input type="submit" value="submit">
					</p>';
}
	
?>